import api from "./api/index"

// app.ts
App<IAppOption>({
  globalData: {
    sites: [],
    isLogin: false,
    userInfo: {
      userId: '',
      userName: '',
      manageSites: [],
      managePoolNos: [],
      phone: ''
    }
  },
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || [];
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录
    api.login({ userNo: '', password: '' }).then(res => {
      if (res.status === 200) {
        this.globalData.isLogin = true;
        this.globalData.userInfo.userId = res.data.userId;
        this.globalData.userInfo.userName = res.data.name;
        this.globalData.userInfo.phone = res.data.phone;
        api.getSite().then(res => {
          const sites: SiteItem[] = res.data;
          this.globalData.sites = sites;
          // 查询该用户管理的场
          const manageSites = this.globalData.sites?.filter(item => (
            item.custodianId === this.globalData.userInfo?.userId
          ))
          const managePools: string[] = [];
          manageSites.forEach((site: SiteItem) => {
            site.pools.forEach((pool: PoolItem) => {
              managePools.push(pool.poolNo);
            })
          });
          this.globalData.userInfo.manageSites = manageSites;
          this.globalData.userInfo.managePoolNos = managePools;
        })
      }
    })
    wx.login({
      success: res => {
        console.log(res.status)
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      },
    })
  },
})